<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {

    /**
     * 登录页面
     */
    public function login()
    {
        $this->display('login');//渲染视图
    }

    /**
     * 执行登录
     */
    public function loginadd()
    {
        $post = $_POST;
//        print_r($post);die;
        $username = isset($_POST['username']) ? trim($_POST['username']) : '';
        $password = isset($_POST['password']) ? trim($_POST['password']) : '';
        $code = isset($_POST['code']) ? trim($_POST['code']) : '';
        $remember = isset($_POST['remember']) ? $_POST['remember'] : 0;

        if ($username && $password == '') {
            $arr = array(
                "code"=>4,
                "msg"=>"用户名密码必填",
            );
            $this->ajaxReturn($arr);
        }
        if (!$this->check_verify($code)) {
            $arr = array(
                "code"=>3,
                "msg"=>"验证码错误",
            );
            $this->ajaxReturn($arr);
        }
        $model = M("admin_user");
        $user = $model->where(array('tel' => $username, 'pwd' => $password))->find();
//        print_r($User);
        if($user){
            $this->action_log("登录操作",$user['admin_id'],true);
            session('admin',$user);
            cookie('admin',$user);
            $arr = array(
                "code"=>0,
                "msg"=>"登录成功",
            );
        }else{
            $arr = array(
                "code"=>"1",
                "msg"=>"您输入的账号或密码不对",
            );
        }
        $this->ajaxReturn($arr);
    }

    /**
     * 退出登录
     */
    public function layout(){
        session(null);
        $this->redirect('Login/Login');
    }

    /**
     * @param $code
     * @param string $id
     * @return bool
     * 验证码检测
     */
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }


    /**
     * 生成验证码
     */
    public function verify(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }



    /**
     * @param string $info
     * @param string $userid
     * @param bool $status
     * 记录管理员用户的行为日志信息
     */
    function action_log($info='',$userid='',$status=true){

        $data['url'] = substr(__ACTION__, strpos(__ACTION__, 'index.php')+strlen('index.php')+1);

        $data['url']=strtolower($data['url']);

        if($userid){
            $data['operator'] =$userid;
        }else{
            $data['operator'] =$_SESSION["admin.admin_id"];
        }

        $data['operate_time'] = NOW_TIME;

        $data['ip']=get_client_ip();

//        $node = M('auth_rule')->where(array('name'=>$data['url']))->find();//查找节点名称

        if($status){

            $data['status']=0;

        }else{

            $data['status']=1;

        }

        if(!empty($info)){

            if($info){

                $data['description']=$info;

            }

            $data['url']=__ACTION__;

            M('cgw_logs')->add($data);//记录日志

        }

    }


    /**
     * 修改密码页面
     */
    public function psw(){
        $this->display('Login/psw');//渲染视图
    }

    /**
     * 修改密码执行
     */
    public function pswsave(){
        $admin_id = session('admin.admin_id');
        $pwd = session('admin.pwd');

        $get = $_GET;

        $user = M('admin_user');
        if($pwd != $get['password']){
            $arr = array(
                "code"=>1,
                "msg"=>"您输入的当前密码有误",
            );
            $this->ajaxReturn($arr);
        }elseif($get['pwd'] == $pwd){
            $arr = array(
                "code"=>0,
                "msg"=>"新密码和旧密码一致",
            );
            $this->ajaxReturn($arr);
        }
        $arr['pwd'] = $get['pwd'];
//        print_r($arr);die;
        $data = $user->where("admin_id = $admin_id")->save($arr);
//        print_r($get);
        if($data){
            $arr = array(
                "code"=>200,
                "msg"=>"修改密码成功",
            );
        }else{
            $arr = array(
                "code"=>0,
                "msg"=>"修改密码失败",
            );
        }
        $this->ajaxReturn($arr);
    }


}

